<?php

Class News {
	
	/**
	 * Unique ID of the news.
	 * 
	 * @access private int
	 */
	private $id;
	
	
	/**
	 * Title of the news.
	 *
	 * @access private string
	 */
	private $title;
	
	
	/**
	 * Unique ID of the author.
	 *
	 * @access private int
	 */
	private $author;
	
	
	/**
	 * Formatted date of the writing of the news.
	 *
	 * @access private string
	 */
	private $creationDate;
	
	
	/**
	 * Formatted date of the last modification bring to the news.
	 * 
	 * @access private string
	 */
	private $lastModification;

	
	
	public function __construct(){
	}
	
	/**
	 * Save a new news or the modifications brought to a news.
	 *
	 * @return bool Return TRUE if success.
	 * @throws Exception Throw exception if fail.
	 */
	public function save(){
	}
	
	/**
	 * Load informations of the news.
	 *
	 * @param int $news Unique ID of a new.
	 * @return bool Return TRUE if success.
	 * @throws Exception Throw exception if fail.
	 */
	public function load($start, $quantity, $news = NULL){
		
		if($news == NULL){
		
			$mysql = MySQL->getInstance();
			
			$query = sprintf(
				"SELECT	N.id,
						N.title,
						N.author,
						N.content,
						N.writening_date,
						N.modification_date,
						N.locale
				FROM	news N
				WHERE	locale	= '%s'
				LIMIT	%d, %d;",
				$_SESSION['localisation'],
				$start,
				$quantity
			);
			
			$ressource = $mysql->query($sql);
			
			if($mysql->error){
				throw new Exception("La récupération des actualités ".
									"a provoqué une erreur SQL. ".
									$mysql->error.".");
			}

			if($ressource->num_rows == 0){
				return NULL;
			}
			else {
				$news = array();
				
				while($result = $ressource->fetch_object()){
					
					$aNews = new News();
					$aNews->setTitle($result->title);
					$news[] = $this;
						
				}
			}
		}
		
		
	}
	
	
	/**
	 * Get the unique ID of a news.
	 *
	 * @return int The unique id of the news.
	 */
	public function getId(){
		return $this->id;
	}

	
	/**
	 * Get the title of a news.
	 *
	 * @return string The title of the news.
	 */
	public function getTitle(){
		return $this->title;
	}
	
	
	/**
	 * Set the title of a news. It is called before a save.
	 *
	 * @param string Thie title of the news.
	 */
	public function setTitle($title){
		$this->title = $title;
	}
	
	
	/**
	 * Get the author of a news.
	 *
	 * @return int The unique ID of the author. 
	 */
	public function getAuthor(){
		return $this->author;
	}
	
	
	/**
	 * Set the author of the news.
	 *
	 * @param int The unique ID of the author.
	 */
	public function setAuthor($author){
		$this->author = $author;
	}
	
	
	/**
	 * Get the formatted date of writening of the news.
	 *
	 * @return string The date of writening of the news.
	 */
	public function getCreationDate(){
		return $this->creation;
	}
	
	
	/**
	 * Get the formatted date of the last modification brought to the news.
	 *
	 * @return string The date of the last modification brought to the news.
	 */
	public function getLastModification(){
		return $this->lastModification();
	}
	
}

?>